import apiRequest from '@/utils/apiRequest';

/**
 * 获取采购到货单列表
 * @param {Object} params - 查询参数
 * @param {number} params.page - 页码
 * @param {number} params.pageSize - 每页数量
 * @param {string} params.receiptCode - 到货单编码
 * @param {string} params.purchaseOrderCode - 采购订单编码
 * @param {string} params.supplierId - 供应商ID
 * @param {string} params.status - 状态
 * @param {string} params.startDate - 开始日期
 * @param {string} params.endDate - 结束日期
 */
export const getPurchaseReceipts = async (params) => {
  try {
    const response = await apiRequest.get('/api/PurchaseReceipts', { params });
    return response.data;
  } catch (error) {
    console.error('Failed to get purchase receipts:', error);
    throw error;
  }
};

/**
 * 获取采购到货单详情
 * @param {string} id - 采购到货单ID
 */
export const getPurchaseReceiptDetail = async (id) => {
  try {
    const response = await apiRequest.get(`/api/PurchaseReceipts/${id}`);
    return response.data;
  } catch (error) {
    console.error(`Failed to get purchase receipt detail ${id}:`, error);
    throw error;
  }
};

/**
 * 创建采购到货单
 * @param {Object} receiptData - 采购到货单数据
 * @param {string} receiptData.receiptCode - 到货单编码
 * @param {string} receiptData.purchaseOrderId - 采购订单ID
 * @param {string} receiptData.supplierId - 供应商ID
 * @param {string} receiptData.receiptDate - 到货日期
 * @param {string} receiptData.warehouseId - 仓库ID
 * @param {string} receiptData.remark - 备注
 * @param {Array} receiptData.items - 到货单明细
 */
export const createPurchaseReceipt = async (receiptData) => {
  try {
    const response = await apiRequest.post('/api/PurchaseReceipts', receiptData);
    return response.data;
  } catch (error) {
    console.error('Failed to create purchase receipt:', error);
    throw error;
  }
};

/**
 * 更新采购到货单
 * @param {string} id - 采购到货单ID
 * @param {Object} receiptData - 采购到货单数据
 * @param {string} receiptData.receiptCode - 到货单编码
 * @param {string} receiptData.purchaseOrderId - 采购订单ID
 * @param {string} receiptData.supplierId - 供应商ID
 * @param {string} receiptData.receiptDate - 到货日期
 * @param {string} receiptData.warehouseId - 仓库ID
 * @param {string} receiptData.remark - 备注
 * @param {Array} receiptData.items - 到货单明细
 */
export const updatePurchaseReceipt = async (id, receiptData) => {
  try {
    const response = await apiRequest.put(`/api/PurchaseReceipts/${id}`, receiptData);
    return response.data;
  } catch (error) {
    console.error(`Failed to update purchase receipt ${id}:`, error);
    throw error;
  }
};

/**
 * 删除采购到货单
 * @param {string} id - 采购到货单ID
 */
export const deletePurchaseReceipt = async (id) => {
  try {
    const response = await apiRequest.delete(`/api/PurchaseReceipts/${id}`);
    return response.data;
  } catch (error) {
    console.error(`Failed to delete purchase receipt ${id}:`, error);
    throw error;
  }
};

/**
 * 审核采购到货单
 * @param {string} id - 采购到货单ID
 */
export const approvePurchaseReceipt = async (id) => {
  try {
    const response = await apiRequest.patch(`/api/PurchaseReceipts/${id}/approve`);
    return response.data;
  } catch (error) {
    console.error(`Failed to approve purchase receipt ${id}:`, error);
    throw error;
  }
};

/**
 * 驳回采购到货单
 * @param {string} id - 采购到货单ID
 * @param {Object} rejectData - 驳回数据
 * @param {string} rejectData.reason - 驳回原因
 */
export const rejectPurchaseReceipt = async (id, rejectData) => {
  try {
    const response = await apiRequest.patch(`/api/PurchaseReceipts/${id}/reject`, rejectData);
    return response.data;
  } catch (error) {
    console.error(`Failed to reject purchase receipt ${id}:`, error);
    throw error;
  }
};